
package jtrimet;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

class ResponseConverter implements Converter 
{

	public ResponseConverter()
	{
		super();
	}
	
	public void marshal(Object source, HierarchicalStreamWriter writer,
			MarshallingContext context)
	{
		// not implemented
	}

	public Object unmarshal(HierarchicalStreamReader reader,
			UnmarshallingContext context)
	{
		Response r = new Response();
		
		
        while (reader.hasMoreChildren()) 
        {
        	
                reader.moveDown();
                
                String nodeName = reader.getNodeName();
                
                if ("location".equalsIgnoreCase(nodeName)) 
                {
                        Location loc = (Location)context.convertAnother(r, Location.class);
                        r.addLocation(loc);
                }
                else if ("arrival".equalsIgnoreCase(nodeName)) 
                {
                        Arrival a = (Arrival)context.convertAnother(r, Arrival.class);
                        r.addArrival(a);
                }
                
                reader.moveUp();
        }
        
        return r;
	}

	public boolean canConvert(Class type)
	{
		return Response.class.isAssignableFrom(type);
	}

}
